package com.weitu.risk.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.weitu.risk.entity.RecogniseDetail;
import com.weitu.risk.mapper.RecogniseDetailMapper;
import com.weitu.risk.service.RecogniseDetailService;
import java.util.List;
import java.util.Map;

import com.weitu.util.DateTimeUtil;
import com.weitu.util.IDGenerator;
import org.apache.commons.collections.map.HashedMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class RecogniseDetailServiceImpl implements RecogniseDetailService {
    @Autowired
    private RecogniseDetailMapper recogniseDetailMapper;

    @Override
    @Transactional
    public boolean addRecogniseDetail(Map<String, Object> map) {
        String listStr = "";
        if(map.get("detailList")!=null){
            listStr = map.get("detailList").toString();
        }
        List<Map> list = JSONArray.parseArray(listStr,Map.class);
        int result = 1;
        if(list.size()>0){
            for(Map m : list){
                String uuid = IDGenerator.getId();
                m.put("id",uuid);
                m.put("addTime", DateTimeUtil.formatterDateTime());
                //添加风险类型
                Map<String,Object> rtMap = new HashedMap();
                rtMap.put("recogniseId",map.get("id").toString());
                rtMap.put("recogniseDetailId",uuid);
                rtMap.put("list",m.get("riskTypeId").toString().split(","));
                //添加风险类型数据
                this.insertRecogniseRiskType(rtMap);
            }
            Map<String,Object> detaiMap = new HashedMap();
            detaiMap.put("list",list);
            detaiMap.put("recogniseId",map.get("id").toString());
            result = this.recogniseDetailMapper.inserRecogniseDetail(detaiMap);
        }
        if(result<1){
            return false;
        }
        return true;
    }
    /**
     * 编辑风险评价详情信息
     * @param map
     * @return
     */
    @Transactional
    public boolean editRecogniseDetail(Map<String,Object> map){
        //删除
        String recogniseId = map.get("id").toString();
        int delResult = this.recogniseDetailMapper.deleteRecogniseDetailByRecogniseId(recogniseId);
        //删除详情对应的风险类型
        int delRiskType = this.recogniseDetailMapper.deleteRcogniseRiskType(recogniseId);
        //添加
        boolean addResult = this.addRecogniseDetail(map);

        if((delResult>=0)&&(delRiskType>=0)&&addResult){
            return true;
        }
        return false;
    }
    @Override
    public boolean deleteRecogniseDetail(Map<String, Object> map) {
        return false;
    }

    @Override
    public List<RecogniseDetail> queryRecogniseDetailList(Map<String, Object> map) {
        return this.recogniseDetailMapper.queryRecogniseDetailList(map);
    }
    /**
     * 查询风险评价信息列表总数
     * @param map
     * @return
     */
    public int queryRecogniseDetailListCount(Map<String,Object> map){
        return this.recogniseDetailMapper.queryRecogniseDetailListCount(map);
    }

    @Override
    public boolean insertRecogniseRiskType(Map<String, Object> map) {
        //map中存放recogniseDetailId 和 list(用户id的集合)
        int result = this.recogniseDetailMapper.insertRecogniseRiskType(map);
        if(result<0){
            return false;
        }
        return true;
    }

    @Override
    public boolean editRcogniseRiskType(Map<String, Object> map) {
        //map中存放recogniseDetailId和 list(用户id的集合)
        String recogniseId = map.get("recogniseId").toString();
        //删除
        int delResult = this.recogniseDetailMapper.deleteRcogniseRiskType(recogniseId);
        //
        //新增
        int addResult = this.recogniseDetailMapper.insertRecogniseRiskType(map);
        if(delResult>=0&&addResult>=0){
            return true;
        }
        return false;
    }
    /**
     * 通过id查询详情信息
     */
    public Map<String,Object> queryRecogniseDetailById(String id){
        return this.recogniseDetailMapper.queryRecogniseDetailById(id);
    }
}